home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / emula / arosdv19.lha / AROS / exec / initstruct.c < prev    next >
C/C++ Source or Header  |  1996-10-24  |  6KB  |  272 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: initstruct.c,v 1.9 1996/10/24 15:50:51 aros Exp $
  4.     $Log: initstruct.c,v $
  5.     Revision 1.9  1996/10/24 15:50:51  aros
  6.     Use the official AROS macros over the __AROS versions.
  7.  
  8.     Revision 1.8  1996/10/23 14:21:29  aros
  9.     Renamed a few macros from XYZ to AROS_XYZ so we know which if from AROS and
  10.     which not.
  11.  
  12.     Revision 1.7  1996/10/21 20:53:16  aros
  13.     Changed BIG_ENDIAN to AROS_BIG_ENDIAN
  14.  
  15.     Revision 1.6  1996/10/19 17:07:26  aros
  16.     Include <aros/machine.h> instead of machine.h
  17.  
  18.     Revision 1.5  1996/09/13 17:51:23  digulla
  19.     Use IPTR
  20.  
  21.     Revision 1.4  1996/08/13 13:56:03  digulla
  22.     Replaced AROS_LA by AROS_LHA
  23.     Replaced some AROS_LH*I by AROS_LH*
  24.     Sorted and added includes
  25.  
  26.     Revision 1.3  1996/08/01 17:41:13  digulla
  27.     Added standard header for all files
  28.  
  29.     Desc:
  30.     Lang: english
  31. */
  32. #include "exec_intern.h"
  33. #include <aros/libcall.h>
  34. #include <exec/alerts.h>
  35. #include <aros/machine.h>
  36.  
  37. /*****************************************************************************
  38.  
  39.     NAME */
  40.     #include <clib/exec_protos.h>
  41.  
  42.     AROS_LH3(void, InitStruct,
  43.  
  44. /*  SYNOPSIS */
  45.     AROS_LHA(APTR,  initTable, A1),
  46.     AROS_LHA(APTR,  memory,    A2),
  47.     AROS_LHA(ULONG, size,      D0),
  48.  
  49. /*  LOCATION */
  50.     struct ExecBase *, SysBase, 13, Exec)
  51.  
  52. /*  FUNCTION
  53.     Initialize some library base or other structure depending on the
  54.     information in the init table. The init table consists of
  55.     instructions starting with an action byte followed by more
  56.     information. The instruction byte looks like:
  57.  
  58.     iisscccc where ii is the instruction code:
  59.               0 - copy following c+1 elements
  60.               1 - repeat following element c+1 times
  61.               2 - take next byte as offset, then copy
  62.               3 - take the next 3 bytes (in the machine's
  63.                   particular byte ordering) as offset, then
  64.                   copy
  65.                ss is the element size
  66.               0 - LONGs
  67.               1 - WORDs
  68.               2 - BYTEs
  69.                cccc is the element count-1
  70.  
  71.     Instruction bytes must follow the same alignement restrictions as LONGs,
  72.     the following elements are aligned to their particular restrictions.
  73.  
  74.     A 0 instruction ends the init table.
  75.  
  76.     INPUTS
  77.     initTable - Pointer to init table.
  78.     memory      - Pointer to uninitialized structure.
  79.     size      - Size of memory area to 0 out before decoding or 0
  80.             for no filling.
  81.  
  82.     RESULT
  83.  
  84.     NOTES
  85.  
  86.     EXAMPLE
  87.  
  88.     BUGS
  89.  
  90.     SEE ALSO
  91.  
  92.     INTERNALS
  93.  
  94.     HISTORY
  95.  
  96. ******************************************************************************/
  97. {
  98.     AROS_LIBFUNC_INIT
  99.  
  100.     LONG  cnt;
  101.     ULONG offset=0,src;
  102.     UBYTE *it,*dst;
  103.     int   s,t;
  104.  
  105.     /* Clear Memory area fast. Get number of longs and clear them. */
  106.     cnt=size/sizeof(LONG);
  107.     size&=(sizeof(LONG)-1);
  108.     dst=(UBYTE *)memory;
  109.     if(cnt)
  110.     do
  111.     {
  112.         *(LONG *)dst=0;
  113.         dst+=sizeof(LONG);
  114.     }
  115.     while(--cnt);
  116.  
  117.     /* Clear the rest. */
  118.     cnt=size;
  119.     if(cnt)
  120.     do
  121.         *dst++=0;
  122.     while(--cnt);
  123.  
  124.     it =(UBYTE *)initTable;
  125.     dst=(UBYTE *)memory;
  126.  
  127.     /* As long as there's something to do */
  128.     while(*it!=0)
  129.     {
  130.     /* What to do. */
  131.     t=*it>>6&3;
  132.  
  133.     /* Element size. */
  134.     s=*it>>4&3;
  135.  
  136.     /* Number of things to do (-1). */
  137.     cnt=*it&15;
  138.  
  139.     /* Depending on the action there may be more information */
  140.     switch(t)
  141.     {
  142.         case 0:
  143.         case 1:
  144.         /* Skip the action byte */
  145.         it++;
  146.         break;
  147.         case 2:
  148.         /* Skip the action byte, get the offset */
  149.         it++;
  150.         offset=*it++;
  151.         break;
  152.         case 3:
  153.         /*
  154.             Get 24bit offset. It's the programmer's responsibility
  155.             to align the action byte with a LONG instruction before
  156.             this.
  157.         */
  158. #if AROS_BIG_ENDIAN
  159.         offset=*(ULONG *)it&0xffffff;
  160. #else
  161.         offset=*(ULONG *)it>>8;
  162. #endif
  163.         it+=sizeof(LONG);
  164.         break;
  165.     }
  166.  
  167.     /* Align source and destination pointers */
  168.     switch(s)
  169.     {
  170.         case 0:
  171.         /* Align pointer to LONG requirements */
  172.         it =(UBYTE *)(((IPTR)it +AROS_LONGALIGN-1)&~(AROS_LONGALIGN-1));
  173.         dst=(UBYTE *)(((IPTR)dst+AROS_LONGALIGN-1)&~(AROS_LONGALIGN-1));
  174.         break;
  175.         case 1:
  176.         /* Same for WORDs */
  177.         it =(UBYTE *)(((IPTR)it +AROS_WORDALIGN-1)&~(AROS_WORDALIGN-1));
  178.         dst=(UBYTE *)(((IPTR)dst+AROS_WORDALIGN-1)&~(AROS_WORDALIGN-1));
  179.         break;
  180.         case 2:
  181.         /* Nothing to do for bytes */
  182.         break;
  183.         default:
  184.         /* And an Alert for nibbles ;-) */
  185.         Alert(ACPU_AddressErr);
  186.  
  187.         /*
  188.             Tell the compiler that he doesn't need to
  189.             care about side effects of Alert()
  190.         */
  191.         return;
  192.     }
  193.  
  194.     /* Switch over action */
  195.     switch(t)
  196.     {
  197.         case 2:
  198.         case 3:
  199.         /* Action is: Add offset then copy */
  200.         dst=(BYTE *)memory+offset;
  201.  
  202.         /* Fall through */
  203.         case 0:
  204.         /* Action is: Copy the next <cnt> elements to the current location */
  205.         switch(s)
  206.         {
  207.             case 0:
  208.             /* Copy loop */
  209.             do
  210.             {
  211.                 *(LONG *)dst=*(LONG *)it;
  212.                 dst+=sizeof(LONG);
  213.                 it +=sizeof(LONG);
  214.             }while(--cnt>=0);
  215.             break;
  216.             case 1:
  217.             do
  218.             {
  219.                 *(WORD *)dst=*(WORD *)it;
  220.                 dst+=sizeof(WORD);
  221.                 it +=sizeof(WORD);
  222.             }while(--cnt>=0);
  223.             break;
  224.             case 2:
  225.             do
  226.                 *dst++=*it++;
  227.             while(--cnt>=0);
  228.             break;
  229.         }
  230.         break;
  231.         case 1:
  232.         /* Action is: Repeat the next element <cnt> times */
  233.         switch(s)
  234.         {
  235.             case 0:
  236.             /* Get source */
  237.             src=*(LONG *)it;
  238.             it +=sizeof(LONG);
  239.  
  240.             /* And write it. */
  241.             do
  242.             {
  243.                 *(LONG *)dst=src;
  244.                 dst+=sizeof(LONG);
  245.             }while(--cnt>=0);
  246.             break;
  247.             case 1:
  248.             src=*(WORD *)it;
  249.             it +=sizeof(WORD);
  250.             do
  251.             {
  252.                 *(WORD *)dst=src;
  253.                 dst+=sizeof(WORD);
  254.             }while(--cnt>=0);
  255.             break;
  256.             case 2:
  257.             src=*it++;
  258.             do
  259.                 *dst++=src;
  260.             while(--cnt>=0);
  261.             break;
  262.         }
  263.         break;
  264.     }
  265.  
  266.     /* Align next instruction byte */
  267.     it=(UBYTE *)(((IPTR)it+AROS_LONGALIGN-1)&~(AROS_LONGALIGN-1));
  268.     }
  269.     AROS_LIBFUNC_EXIT
  270. }
  271.  
  272.